@echo off



echo %root%

set num=0

for %%a in (%*) do set /a num+=1

echo arg number:%num%
set arg0=%0
set arg1=%1
set arg2=%2
set arg3=%3
set arg4=%4
set arg5=%5
set arg6=%6

echo arg0=%arg0%
echo arg1=%arg1%
echo arg2=%arg2%
echo arg3=%arg3%
echo arg4=%arg4%
echo arg5=%arg5%
echo arg6=%arg6%

set currentPath=%arg0:~0,-14%
set project_path= %arg1:~1,-1%
set branch= %arg2:~1,-1%
set channel= %arg3:~1,-1%
set apk_path= %arg4:~1,-1%
set jiagu_exe= %arg5:~1,-1%
set output= %arg6:~1,-1%

echo=
echo currentPath =%currentPath%
echo project_path =%project_path%
echo branch =%branch%
echo channel =%channel%
echo apk_path =%apk_path%
echo jiagu_exe =%jiagu_exe%
echo output =%output%

echo=
echo --------进入目录--------
call cd %project_path:~0,4%
call cd %project_path% 

if not "%branch%" == " " (
	echo=
	echo --------获取%branch% 分支代码--------
	call git checkout %branch%
)

if "%channel%" == " " (
	echo=
	echo=
	echo=
	echo --------开始构建--------
	call gradlew assembleRelease

	if errorlevel 1 (
	    echo 发生错误，已终止。
	    goto fail
	)
)

if not "%channel%" == " " (
	for %%i in (%channel%) do (
		echo=
		echo=
		echo=
		echo --------开始构建渠道包%%i--------
		call gradlew assemble%%iRelease

        if errorlevel 1 (
            echo 发生错误，已终止。
            goto fail
        )
	)
)



echo=
echo=
echo=
echo --------开始加固--------
if "%apk_path%" == " " (
    echo --------apk_path为空，本次不加固--------
    goto fail
)

if "%jiagu_exe%" == " " (
    echo --------jiagu_exe为空，本次不加固--------
    goto fail
)

if "%output%" == " " (
    echo --------output为空，本次不加固--------
    goto fail
)

call cd %apk_path:~0,4%
call cd %apk_path%

for %%a in (*.apk) do (

    echo %%a | findstr "jiagu.apk" >null && (
        echo=
    ) || (
        echo=
        echo=
        echo=
        echo --------加固：%%a--------

        ::start java -Dfile.encoding=UTF-8 -jar D:\newcapec\appsafe\360jiagubao_windows_64\jiagu\jiagu.jar -jiagu %%a %output%
        ::start java -Dfile.encoding=UTF-8 -jar %jiagu_exe%\jiagu.jar -jiagu %%a %output%
        start %currentPath%auto_jiagu.bat "%jiagu_exe%" "%%a" "%output%"
    )
)

:fail

echo=
echo=
echo=
pause

exit


